package grouplife::Controller::RenrenLogin;
use Moose;
use Encode;
use namespace::autoclean;
use Catalyst qw/
Session
Session::Store::FastMmap
Session::State::Cookie
/;
use Util::Renren::Authenticate;
use grouplife::Model::Store::StoreSearchModel;
use grouplife::Model::Users::UsersSearchModel;

BEGIN {extends 'Catalyst::Controller'; }
__PACKAGE__->config( 'Plugin::ConfigLoader' => { file => 'grouplife.yml' } );

=head1 NAME

grouplife::Controller::Login - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut


sub index :Path :Args(0) {
	my ($self, $c) = @_;

# Get the username and password from form
	my $code= $c->request->params->{code};
	my $renren_redirect_uri = $c->config->{renren_redirect_uri}; 
	my $renren_app_key = $c->config->{renren_app_key};
	my $renren_secret_key = $c->config->{renren_secret_key};
	my $renren_oauth_url = $c->config->{renren_oauth_url};

	my $authenticator = Util::Renren::Authenticate->new();
	my $user = $authenticator->get_userinfo($code, $renren_app_key, $renren_secret_key, $renren_redirect_uri, $renren_oauth_url);
	if($user->{code} == 0) {
                #my @users = $c->model('Users::UsersSearchModel')->get_user_by_email($user->{email});
		chomp($user->{username});
		chomp($user->{renren_id});
		$c->session->{user_name} = $user->{username};

		my $username = encode_utf8(decode("gbk",$user->{username}));
		my $source_nick = $username."|".$user->{renren_id};	
		my $users = $c->model('Users::UsersSearchModel')->renren_get_user_by_nick($c, 2,$source_nick);

		my $length = scalar(@$users);
		if ($length == 0) {    
			#first time bind renren
                        #Returned stores whose picked = 'R' or 'Y'
                        $c->stash(nick  =>  $user->{username}),
			$c->stash(source_nick => $source_nick),
                        $c->stash(css => 'zhuce.css'),
                        $c->stash(javascript => '/user/form_create.js');
                        $c->stash(template => 'renren/renren_oauth.tt2');
                } else {
                        if( ($source_nick eq $users->[0]->get_column('source_nick')) and ($users->[0]->get_column('source') eq 2)) {
				my $auth_user = $c->find_user({ id => $users->[0]->id });
                                $c->set_authenticated($auth_user);
                                $c->session->{login_user} = $auth_user;
                                $c->response->redirect($c->uri_for($c->controller('Users')->action_for('user_info'), [$users->[0]->id]));
                        } else {
				$c->stash(source_nick => $source_nick),
                                $c->stash(nick  =>  $users->[0]->{username}),
                                $c->stash(email =>  $users->[0]->get_column('email_address')),
                                $c->stash(user =>  $users->[0]),
                                $c->stash(css => 'zhuce.css'),
				$c->stash(template => 'renren/renren_bind.tt2');
			}
		}
	}
}



=head1 AUTHOR

lijie,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
